package com.ruanyun.modular.admin.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.consts.PublicCommon;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.modular.admin.entity.TProductionProcess;
import com.ruanyun.modular.admin.mapper.TCommitReportMapper;
import com.ruanyun.modular.admin.model.result.TProductinfoResult;
import com.ruanyun.sys.core.shiro.ShiroKit;
import com.ruanyun.modular.admin.mapper.TProductionProcessMapper;
import com.ruanyun.modular.admin.model.params.TProductionProcessParam;
import com.ruanyun.modular.admin.model.result.TProductionProcessResult;
import  com.ruanyun.modular.admin.service.TProductionProcessService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanyun.sys.modular.system.entity.User;
import com.ruanyun.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 生产工艺 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-04-17
 */
@Service
public class TProductionProcessServiceImpl extends ServiceImpl<TProductionProcessMapper, TProductionProcess> implements TProductionProcessService {
    @Autowired
    private TCommitReportMapper commitReportMapper;
    @Autowired
    private UserService userService;

    @Override
    public ResponseData add(TProductionProcessParam param){
        TProductionProcess entity = getEntity(param);

        entity.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        if(entity.getCompanyId() ==null ){
            return  ResponseData.error("未选择企业所属企业 清先查看企业信息是否填写完整");
        }
        Integer count = commitReportMapper.getIsCommit(userService.getCid());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法继续添加");
        }

        try {
            this.baseMapper.insert(entity);
        } catch (DuplicateKeyException e){
            return ResponseData.error("请选择其他企业");
        }
        return ResponseData.success();
    }

    @Override
    public void delete(TProductionProcessParam param){
        this.removeById(getKey(param));
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @Override
    public ResponseData updateDelFlagById(Long id) {
        TProductionProcess entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该生产工艺不存在");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_DELETE);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(TProductionProcessParam param){
        TProductionProcess oldEntity = getOldEntity(param);
        TProductionProcess newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        Integer count = commitReportMapper.getIsCommit(oldEntity.getCompanyId());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法修改");
        }
        int result = this.baseMapper.updateById(newEntity);
        if(result == 0){
        return ResponseData.error("修改失败失败");
        }
        return ResponseData.success();
    }

    @Override
    public TProductionProcessResult findBySpec(TProductionProcessParam param){
        return null;
    }

    @Override
    public List<TProductionProcessResult> findListBySpec(TProductionProcessParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TProductionProcessParam param){
        /*判断当前用户是否为管理员*/
        boolean flag = ShiroKit.isAdmin();
        if (flag != true){
            User u = userService.getUserInfo();
            if("1402427539539161089".equals(u.getRoleId())){//市管理账号
                param.setIsArea("y");
            }

            if("1402427690353750018".equals(u.getRoleId()) ){ //县管理账号
                param.setCountyId(String.valueOf(u.getUserId()));
            }

            if("1162247971007717377".equals(u.getRoleId())){ //企业账号
                param.setCompanyId(userService.getCid());
            }
        }
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList1(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TProductionProcessParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TProductionProcess getOldEntity(TProductionProcessParam param) {
        return this.getById(getKey(param));
    }

    private TProductionProcess getEntity(TProductionProcessParam param) {
        TProductionProcess entity = new TProductionProcess();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    /**
        * 启用
        *
        * @param id
        * @return
        */
    @Override
    public ResponseData enableById(Long id) {
        TProductionProcess entity = this.baseMapper.selectById(id);
        if (entity == null) {
        return ResponseData.error("该生产工艺不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("启用失败");
        }
        return ResponseData.success();
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @Override
    public ResponseData disableById(Long id) {
        TProductionProcess entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该生产工艺不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("禁用失败");
        }
        return ResponseData.success();
    }

    @Override
    public Page<TProductionProcessResult> getProductionProcessPage(Integer companyId) {
        Page pageContext = getPageContext();
        Page<TProductionProcessResult>  results =  baseMapper.getProductionProcessPage(pageContext,companyId);
        return results;
    }

    @Override
    public List<TProductionProcessResult> getProcessList(Integer companyId) {
        return baseMapper.getProcessList(companyId);
    }


}
